/*
 * @(#)ConvertToMsSql.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

/**
  zipView.java

  This class is to read the content of a zip file.
  It can either print out the path name of the files in the zip file
  or print out the content of a specified file.
  
  Choice 1: To print out the path names of the files in the zip file
  args[0]: The path name of the zip file
  args[1]: The path name of the output file
  
  Choice 2: To print out the content of the specifed file in the zip file
  args[0]: -contents
  args[1]: The path name of the specified file in the zip file (e.g. pieChart.jsp)
  args[2]: The path name of the zip file
  args[3]: The path name of the output file

    @author          Phoebe Wong
    @company         DCIVision Limited
    @creation date   20/08/2003
    @version         $Revision: 1.3 $
*/
   
public class zipView {
  /**
   * dump
   *
   * Function to print out the content of the specified file
   * in the zip file
   *
   * @param zf           The path name of the zip file
   * @param ze           The zip file entry for the specified name to be printed
   * @param outFileName  The path name of the output file
   * @return             null
   */
  public static void dump(ZipFile zf, ZipEntry ze, String outFileName) throws IOException {
    System.out.println(">>>>> " + ze.getName());    
    InputStream istr = zf.getInputStream(ze);
    BufferedInputStream bis = new BufferedInputStream(istr);
    FileOutputStream fos = new FileOutputStream(outFileName, true);
    int sz = (int)ze.getSize();
    System.out.println("size:"+sz);        
    final int N = 1024;
    byte buf[] = new byte[N];
    int ln = 0;
    while (sz > 0 && (ln = bis.read(buf, 0, Math.min(N, sz))) != -1) {
      System.out.write(buf, 0, ln);
      System.out.print("\n");
      fos.write(buf, 0, ln);
      sz -= ln;
    }
    bis.close();
    fos.flush();
    fos.close();   
  }
   
  public static void main(String args[]) {
    boolean dump_contents = false;
    int arg_indx = 0;
    ZipFile zf = null;
    FileWriter fo = null;
    
    if (args.length >= 1 && args[0].equals("-contents")) {
      dump_contents = true;
      arg_indx+=2;
    }
    if (arg_indx >= args.length) {
      System.err.println("usage: [-contents] file");
      System.exit(1);
    }
   
    try {
      zf = new ZipFile(args[arg_indx]);
      File outputFile = new File(args[arg_indx + 1]);
      fo = new FileWriter(outputFile);    
   
      Enumeration list = zf.entries();
      
      /**
       * To print out the path name of the zip file
       * & print out the number of files in the zip file
       */
      if (!dump_contents) {
        fo.write("Path Name of the ZIP File : " + zf.getName());
        fo.write("\n");
        fo.write("Number of files in the ZIP file: " + zf.size());
        fo.write("\n");
        fo.write("\n");
      }
      
      /**
       * To print out the path name of all files in the zip file
       */
      while (list.hasMoreElements()) {
        ZipEntry ze = (ZipEntry)list.nextElement();
        if (!dump_contents || ze.isDirectory()) {
          fo.write("--> " + ze.getName());
          fo.write("\n");
          int nSize = (int)ze.getSize();
          fo.write("    Uncompressed Size: " + nSize + " bytes");
          fo.write("\n");
          fo.write("    Compressed Size: " + ze.getCompressedSize() + " bytes");
          fo.write("\n");
          fo.write("\n");
          continue;
        }        
      }
      /**
       * To print out the content of a specififed file 
       */
      if (dump_contents) {
        try {
          ZipEntry zeIn = (ZipEntry)zf.getEntry(args[1]);
          dump(zf, zeIn, args[arg_indx + 1]);
        } catch (IOException e) {
          System.err.println("exception: " + e);
        }
      }
    } catch (ZipException e1) {                   
      System.err.println("exception: " + e1);
    } catch (IOException e2) {               
      System.err.println("exception: " + e2);
    } finally {
      try {fo.close();} catch(Exception ignore) {}
    }    
  }
}                                   
